package org.lib.jpamysql.mtm;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;

import lombok.Data;

/**
 * 博客
 * @author ben
 * @date 2021-10-20 09:46:24 CST
 */
@Entity
@Data
public class MtmBlog {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	
	@Column(columnDefinition = "VARCHAR(100) NOT NULL")
	private String title;
	
	@Column(columnDefinition = "VARCHAR(1000) NOT NULL")
	private String content;
	
	@ManyToMany(cascade = {CascadeType.ALL})
	@JoinTable(
			// 不使用name属性，默认关联表名为 mtm_blog_tags
			name = "mtm_blog_tag_rel",
			joinColumns = @JoinColumn(name = "blog_id", referencedColumnName = "id",
					foreignKey = @ForeignKey(name="fk_blog_tag_rel_blog")),
			inverseJoinColumns = @JoinColumn(name = "tag_id", referencedColumnName = "id",
					foreignKey = @ForeignKey(name="fk_blog_tag_rel_tag"))
			)
	// 排序
	@OrderBy(value = "tag DESC")
	private List<MtmBlogTag> tags;
	
}
